package com.hydra.auth.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hydra.auth.dto.SysUserQueryDto;
import com.hydra.auth.entity.SysUser;
import com.hydra.auth.mapper.SysUserMapper;
import com.hydra.auth.repository.SysUserService;
import com.hydra.base.controller.BaseController;
import com.hydra.base.dto.auth.AuthUser;
import com.hydra.base.feign.TestOpenFeignService;
import com.hydra.base.resp.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户的控制器
 * @Author Hydra
 * @Date 2022/5/21 17:42
 */
@Slf4j
@RequestMapping("/api/auth/user/")
@RestController
public class SysUserController extends BaseController<SysUserQueryDto, SysUser, SysUserMapper, SysUserService> {

    @Autowired
    public SysUserController(SysUserService repository){
        super.setRepository(repository);
    }

    /**保存数据之前的操作*/
    @Override
    protected SysUser handleBeforeSave(SysUser sysUser, SysUser history) {
        //字段设置为null，之后在save方法里面就不会被修改了
        sysUser.setUsername(null);
        sysUser.setPassword(null);//密码提供单独的接口去修改
        return sysUser;
    }


    /**
     * 根据code获取用户信息
     * @param codeList 用户code列表
     * @return      列表对象
     */
    @GetMapping(path = "/listAuthUserByCodeList")
    public List<AuthUser> listAuthUserByCodeList(@RequestParam("codeList") List<String> codeList) {
        List<AuthUser> result = new ArrayList<>();
        if(CollectionUtils.isEmpty(codeList)){
            return result;
        }
        List<SysUser> sysUsers = this.repository.listUserByCode(codeList);
        if(!CollectionUtils.isEmpty(sysUsers)){
            for (SysUser userInfo : sysUsers) {
                AuthUser authUser = new AuthUser();
                BeanUtil.copyProperties(userInfo, authUser);
                result.add(authUser);
            }
        }
        return result;
    }


    @Autowired
    private TestOpenFeignService testOpenFeignService;
    /**
     * 测试
     */
    @GetMapping(path = "/test")
    public Object test(){
        Response response = testOpenFeignService.test("1");
        return response.getData();
    }


}
